package com.summer.NIO;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileChannelCase1 {
	public static void main(String[] args) throws IOException {
		RandomAccessFile aFile = new RandomAccessFile("FileChannelCase1.txt", "rw");
		FileChannel channel = aFile.getChannel();
		
//		String newData = "New String to write to file..." + System.currentTimeMillis();
//
//		ByteBuffer buf = ByteBuffer.allocate(48);
//		buf.clear();
//		buf.put(newData.getBytes());
//
//		buf.flip();
//
//		while(buf.hasRemaining()) {
//		    channel.write(buf);
//		}
//		buf.clear();
//		long pos = channel.position();
//		channel.position(pos +123);
//		while(buf.hasRemaining()) {
//		    channel.write(buf);
//		}
//		channel.force(true);

		
//		channel.truncate(20);

		ByteBuffer buf = ByteBuffer.allocate(48);
		int bytesRead = channel.read(buf);
		
		buf.flip();
		byte[] text = new byte[buf.limit()];
		buf.get(text,0,text.length);
		
		long fileSize = channel.size();
		System.out.println("fileSize :"+fileSize);
		
		System.out.println("fileText :"+new String(text));
		
		channel.close();

		
	}
	
}
